package org.teremail.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TereMailClient implements EntryPoint {

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        final MailServiceAsync mailService = 
            (MailServiceAsync) GWT.create(MailService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) mailService;
        endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "/MailService");
        
        final String username = "mike";
        final ErrorHandler eh = new DialogErrorHandler();
        
        final FlexTable folders = new FlexTable();
        final FolderController folderCtl = 
            new FolderController(folders, eh, mailService);
        
        final FlexTable messages = new FlexTable();
        final SummaryController summaryCtl = 
            new SummaryController(messages, eh, mailService);
        
        final TextArea messageText = new TextArea();
        final MessageTextController messageTextCtl = 
            new MessageTextController(messageText, eh, mailService);
        
        // Event handers.
        folders.addTableListener(new TableListener() {
            public void onCellClicked(SourcesTableEvents sender, int row,
                    int cell) {
                if (row != 0) {
                    String folder = folders.getText(row, cell);
                    summaryCtl.fetchMessages(username, folder);
                }
            }
        });
        
        messages.addTableListener(new TableListener() {
            public void onCellClicked(SourcesTableEvents sender, int row,
                    int cell) {
                String folder = summaryCtl.getFolder();
                int uid = summaryCtl.getUid(row);
                messageTextCtl.fetchMessage(username, folder, uid);
            }
        });
        
        RootPanel.get("slot1").add(folders);
        RootPanel.get("slot2").add(messages);
        RootPanel.get("slot3").add(messageText);
        
        folderCtl.fetchFolders(username);
    }
    
    private static class DialogErrorHandler implements ErrorHandler {
        public void showMessage(String message) {
            final MyDialog d = new MyDialog(message);
            d.setPopupPositionAndShow(new PositionCallback() {
                public void setPosition(int offsetWidth, int offsetHeight) {
                    int left = (Window.getClientWidth() - offsetWidth) / 3;
                    int top = (Window.getClientHeight() - offsetHeight) / 3;
                    d.setPopupPosition(left, top);
                }
            });
        }
    }
    
    private static class MyDialog extends DialogBox {

        public MyDialog(String message) {
            // Set the dialog box's caption.
            setText(message);

            Button ok = new Button("OK");
            ok.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    MyDialog.this.hide();
                }
            });
            setWidget(ok);
            addStyleName("bordered");
        }
        
    }
}
